import { FC, SetStateAction, useState } from 'react'
import { EMPTYTEXT, useNewPage } from '@hs-admin/utils'
import { HsAdminTablePage } from '@hs-admin/base-components'
import { postCompanyInfoDetailQueryCompetitor } from '@/api/companyDetail/MarketConditions'
import { Button } from 'antd'
import { openCompanyDetail } from 'hs-react-company'

interface Props {
  companyCode: string
}
const CompetingCompanies: FC<Props> = (props: Props) => {
  const { companyCode } = props
  const columns = [
    {
      title: '序号',
      dataIndex: 'index',
      width: 80,
      render: (_: string, _record: any, index: number) => <>{index + 1 + (pagination.current - 1) * pagination.pageSize}</>
    },
    {
      title: '竞争公司',
      dataIndex: 'compCompanyName',
      render: (text: string, record: any) => (
        <Button type="link" onClick={() => openCompanyDetail(record.compCompanyCode)}>
          {text}
        </Button>
      )
    },
    {
      title: '竞品名称',
      dataIndex: 'compProduct'
    },
    {
      title: '竞争业务方向',
      dataIndex: 'compProductDescri'
    },
    {
      title: '所在城市',
      dataIndex: 'compCity'
    },
    {
      title: '成立时间',
      dataIndex: 'compFoundDate'
    },
    {
      title: '注册资本',
      dataIndex: 'compRegcapitalAmtCal',
      render: (text: number) => <>{text / 10000 + '万元' || EMPTYTEXT}</>
    }
  ]
  const { pagination } = useNewPage(callList)
  const [data, setdata] = useState([])
  const [isShow, setIsShow] = useState(false)

  function callList() {
    return postCompanyInfoDetailQueryCompetitor({
      pageNo: pagination.current,
      pageSize: pagination.pageSize,
      companyCode: companyCode || 'ce4544a026a9766208f9ba12860f3121'
    }).then((res: { data: { list: SetStateAction<never[]>; total: any } }) => {
      setdata(res.data.list)
      setIsShow(res.data.total ? true : false)
      return res.data
    })
  }

  return (
    <>
      {isShow && (
        <div className="market-conditions-CompetingCompanies-wrapper" id="竞争公司">
          <h3 className="mb-4">竞争公司</h3>
          <HsAdminTablePage
            columns={columns}
            dataSource={data}
            id="market-conditions-CompetingCompanies"
            pagination={pagination}
            rowKey={(record: { compCompanyName: string }) => record.compCompanyName}
            size="small"
          />
        </div>
      )}
    </>
  )
}

export default CompetingCompanies
